/**
 * Copyright (c) 2011 by Kevin Bierhoff.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *    1. Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *    3. Neither the names of the authors nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.googlecode.syper.test.javasyp.logic;

import com.googlecode.syper.anno.Excl;
import com.googlecode.syper.anno.Pred;
import com.googlecode.syper.anno.Result;

import edu.cmu.cs.crystal.annotations.PassingTest;
import edu.cmu.cs.crystal.annotations.UseAnalyses;

/**
 * Tests array and object permissions nested into local arrays
 * TODO support calls on right-hand side of array assignments
 * @author kevin
 * @since Nov 13, 2011
 */
@PassingTest
@UseAnalyses("SymbolicChecker")
public class NestedLocalArrays {

	/** assign into nested array and make sure the value stays */
	public static void simpleTest() {
		@Excl(elems = "excl")
		int[][] matrix = new int[5][5];
		matrix[0][0] = 1;
		assert matrix[0][0] == 1;
		id(0);
		assert matrix[0][0] == 1;
	}

	/** test protecting nested local arrays */
	public static void localIntLists() {
		@Excl(elems = "excl") int[][] lists = new int[5][];
		
		@Excl(borrowed = false) int[] list = new int[] { 1, 2, 3 };
		assert list[0] == 1;
		assert lists[0] == null;
		id(1);
		assert list[0] == 1;
		assert lists[0] == null;
		
		lists[0] = list;
		assert list[0] == 1;
		assert lists[0] != null;
		assert lists[0][0] == 1;
		
		id(2);
		assert list[0] == 1;
		assert lists[0] != null;
		assert lists[0][0] == 1;
		
		lists[1] = new int[] { 4, 5, 6 };
		assert list[0] == 1;
		assert lists[0] != null;
		assert lists[1] != null;
		assert lists[0][0] == 1;
		assert lists[1][0] == 4;
		
		@Excl(borrowed = false) final int[] l3 = new int[] { id(7), 8, 9 };
		lists[2] = l3; // TODO = new int[] { id(7), 8, 9 };
		assert list[0] == 1;
		assert lists[0] != null;
		assert lists[1] != null;
		assert lists[2] != null;
		assert lists[0][0] == 1;
		assert lists[1][0] == 4;
		assert lists[2][0] == 7;
	}

	/** test protecting fields of objects in arrays across calls */
	public static void pointArray() {
		@Excl(elems = "excl") Point2D[] points = new Point2D[2];
		assert points[0] == null & points[1] == null;
		@Excl(borrowed = false) Point2D p1 = new Point2D(3, 5);
		points[0] = p1;// TODO new Point2D(3, 5);
		assert points[0] != null;
		assert points[1] == null;
		assert points[0].x == 3 & points[0].y == 5;
		@Excl(borrowed = false) Point2D p2 = new Point2D(6, 10);
		points[1] = p2;// TODO new Point2D(6, 10);
		assert points[0] != null;
		assert points[1] == p2;
		assert points[0].x == 3 & points[0].y == 5;
		assert points[1].x == 6 & points[1].y == 10;
		id(3);
		assert points[0] != null;
		assert points[1] == p2;
		assert points[0].x == 3 & points[0].y == 5;
		assert points[1].x == 6 & points[1].y == 10;
	}

	/** marker method for easier debugging */
	private static int id(int i) {
		@Result final int result = i;
		Pred.ensures(result == i);
		return result; 
	}
	
//	@ReturnExcl private static <T> T id(@Excl(borrowed = false) T o) {
//		@Result @Excl(borrowed = false) final T result = o;
//		Pred.ensures(result == o);
//		return result; 
//	}
//
//	@ReturnExcl
//	private static Point2D origin() {
//		@Result @Excl(borrowed = false) final Point2D result = new Point2D(0, 0);
//		Pred.ensures(result != null);
//		return result;
//	}

	private static final class Point2D {
		private int x;
		private int y;
		
		public Point2D(int x, int y) { 
			this.x = x; 
			this.y = y;
			Pred.ensures(this.x == x & this.y == y);
		}
	}
}
